package com.example.picvideodemo;

import android.Manifest;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.picvideodemo.utils.DownloadUtil;
import com.example.picvideodemo.view.ImageViewPager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShowImageVideoActivity extends AppCompatActivity {

    @BindView(R.id.vp_img)
    ImageViewPager vpImg;
    @BindView(R.id.txt_page)
    TextView txtPage;
    @BindView(R.id.con_image)
    ConstraintLayout conImage;
    @BindView(R.id.video_view)
    VideoView videoView;
    @BindView(R.id.video_image)
    ImageView videoImage;
    @BindView(R.id.rl_video)
    RelativeLayout rlVideo;
    @BindView(R.id.con_layout)
    ConstraintLayout conLayout;

    private ArrayList<String> mImage;
    private String mPosition;
    private String type;
    private String time;
    private String path;
    private MediaPlayer mMediaPlayer;
    private String mSubstring;
    private String mName;
    private String source;

    //    public LoadDialog mLoadDialog;
    //接收图片显示
    public static void actionStart(Context context, List<String> image, String index, String type, String source) {
        Intent intent = new Intent(context, ShowImageVideoActivity.class);
        intent.putStringArrayListExtra("image", (ArrayList<String>) image);
        intent.putExtra("index", index);
        intent.putExtra("type", type);
        intent.putExtra("source", source);
        context.startActivity(intent);
    }

    //视频显示
    public static void actionStart(Context context, String path, String type, String time, String source) {
        Intent intent = new Intent(context, ShowImageVideoActivity.class);
        intent.putExtra("path", path);
        intent.putExtra("type", type);
        intent.putExtra("time", time);
        intent.putExtra("source", source);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_image_video);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mImage = getIntent().getStringArrayListExtra("image");
        mPosition = getIntent().getStringExtra("index");
        path = getIntent().getStringExtra("path");
        type = getIntent().getStringExtra("type");
        time = getIntent().getStringExtra("time");
        source = getIntent().getStringExtra("source");
        //查看类型video视频，image图片
        if (type.equals("video")) {
            rlVideo.setVisibility(View.VISIBLE);
            conImage.setVisibility(View.GONE);
            playSound(path);
            return;
        }
        if (type.equals("image")) {
            conImage.setVisibility(View.VISIBLE);
            rlVideo.setVisibility(View.GONE);
            initImage();
            return;
        }
    }

    private void initImage() {
        updata(Integer.parseInt(mPosition) + 1 + "");
        ImgViewPagerAdapter adapter = new ImgViewPagerAdapter(mImage);
        vpImg.setAdapter(adapter);
        vpImg.setCurrentItem(Integer.parseInt(mPosition));
        vpImg.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                updata(position + 1 + "");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (videoView != null && videoView.isPlaying()) {
            videoView.pause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null && videoView.isPlaying()) {
            videoView.pause();
            videoView.stopPlayback();
        }
//        if (mLoadDialog != null) {
//            mLoadDialog.dismiss();
//        }
    }


    /**
     * 当前查看页数
     *
     * @param position
     */
    private void updata(String position) {
        txtPage.setText(position + "/" + mImage.size());
    }


    @OnClick({R.id.con_layout})
    public void onViewClicked() {
        finish();
    }

    public class ImgViewPagerAdapter extends PagerAdapter {
        private List<String> imageurl;


        public ImgViewPagerAdapter(List<String> imageurl) {
            this.imageurl = imageurl;

        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            ImageView view = new ImageView(container.getContext());
            Glide.with(container.getContext()).load(mImage.get(position)).into(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return imageurl.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    //播放
    private void playSound(String path) {
//        if (mLoadDialog == null) {
//            mLoadDialog = new LoadDialog(ShowImageVideoActivity.this, "正在加载");
//        }
//        mLoadDialog.show();
        System.out.println("路径" + path);
        MediaController media = new MediaController(this);
        Uri uri = Uri.parse(path);
        videoView.setVideoURI(uri);
        videoView.setMediaController(media);
        media.setAnchorView(videoView);
        videoView.start();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                    @Override
                    public boolean onInfo(MediaPlayer mp, int what, int extra) {
                        /* The player just pushed the very first video frame for rendering.
                         * 视频第一帧开始渲染,视频真正开始播放.
                         * @see android.media.MediaPlayer.OnInfoListener
                         */
                        if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                            new Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    //隐藏预览图片,这里延时100ms消失是防止页面过渡时闪屏
                                    videoImage.setVisibility(View.INVISIBLE);
//                                    if (mLoadDialog != null && mLoadDialog.isShowing()) {
//                                        mLoadDialog.dismiss();
//                                    }
                                }
                            }, 100);
                            return true;
                        }
                        return false;
                    }
                });
            }
        });
    }


}
